# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
from backend import LOG
from backend.commons import catch_exception
from backend.exceptions import BusinessServiceException
from ..model import business_request_log_manager
from ..business_service.detection import DetectRequest


class Display(object):
    """
    业务服务请求展示接口
    """

    TIMEOUT = None
    TIMEOUT_HANDLER = None

    def __init__(self, request):
        # 简单工厂
        self.request = request
        self.data = dict()
        if isinstance(self.request, DetectRequest):
            self.display_detection()
        else:
            raise BusinessServiceException(message=u"无法实例化数据展示接口")

    @catch_exception
    def display_detection(self):
        # quality related
        self.data["is_normal_shelf"] = self.request.is_normal_shelf
        self.data[
            "is_normal_shelf_confidence"
        ] = self.request.is_normal_shelf_confidence
        self.data["is_normal_shelf_coordinate"] = self.request.is_normal_shelf_coordinate

        # statistics
        # self.data["shelf_num"] = 0  # TODO: 更新general服务，返回shelf相关信息后再展示出来
        self.data["statistics"] = len(self.request.search_results)
        self.data["unknown"] = 0
        sku_set = set()
        for item in self.request.search_results:
            if "others" in item[4]:
                self.data["unknown"] += 1
            else:
                sku_set.add(item[4])
        sku_set.add("others")
        sku_set.add("others竞品")
        sku_set.add("others生动化")

        # each sku related
        self.data["each_sku"] = list()
        LOG.info("处理每个SKU数据展示")
        for item in self.request.search_results:
            sku_name = item[4]
            if sku_name in self.request.sku.skuinfos.keys():
                sku_info = self.request.sku.skuinfos[sku_name]
                each_sku = dict(
                    brand=sku_info["brand"],
                    # cate=sku_info["cate"],
                    # shelf_num=None,  # TODO: 更新general服务，返回shelf相关信息后再展示出来
                    # number_in_pic=(list(sku_set)).index(item[4]) + 1,
                    name=sku_info["name"],
                    # number=sku_info["number"],
                    # quantity=1,
                    # shape=sku_info["shape"],
                    # spec=sku_info["spec"],
                    # unitprice=sku_info["unitprice"],
                    sku_coordinate=[
                        item[1],
                        item[0],
                        item[3],
                        item[2],
                    ],  # TODO: AI服务返回值更新次序后，更新此逻辑
                    sku_confidence=[{"top1": [item[4], item[5]]}],
                )
            else:
                each_sku = dict(
                    brand="",
                    # cate="",
                    # shelf_num=None,  # TODO: 更新general服务，返回shelf相关信息后再展示出来
                    # number_in_pic=(list(sku_set)).index(item[4]) + 1,
                    name="未知",
                    # number="",
                    # quantity=1,
                    # shape="",
                    # spec="",
                    # unitprice="",
                    sku_coordinate=[
                        item[1],
                        item[0],
                        item[3],
                        item[2],
                    ],  # TODO: AI服务返回值更新次序后，更新此逻辑
                    sku_confidence=[{"top1": [item[4], item[5]]}],
                )
            self.data["each_sku"].append(each_sku)

        # img width & height from request
        if "img_width" in self.request.ext:
            self.data["source_img_width"] = self.request.ext["img_width"]
        if "img_height" in self.request.ext:
            self.data["source_img_height"] = self.request.ext["img_height"]
        business_request_log_manager.update(self.request.business_request_log, display_result=self.data)

